package com.qfoud.edu.aunt.commons.dao.impl;

import com.qfoud.edu.aunt.commons.dao.IUserDAO;
import com.qfoud.edu.aunt.commons.pojo.User;
import com.qfoud.edu.aunt.commons.utils.JdbcUtils;
import com.qfoud.edu.dbutils.framwork.result.impl.BeanHandler;
import com.qfoud.edu.dbutils.framwork.result.impl.BeanListHandler;

import java.util.List;

/**
 * @author xiaobobo
 * @title: UserDAO
 * @projectName cd-java-2303-project-parent
 * @description: TODO
 * @date 2023/5/11  9:07
 */
public class UserDAO implements IUserDAO {
    @Override
    public List<User> list() throws Exception {
        return JdbcUtils.queryRunner().query("select * from t_user",new BeanListHandler<User>(User.class));
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        JdbcUtils.queryRunner().update("delete from t_user where id=?",id);
    }

    @Override
    public User findUserById(Integer id) throws Exception {
        return JdbcUtils.queryRunner().query(
                "select * from t_user where id=?",new BeanHandler<User>(User.class),id);
    }

    @Override
    public void update(User user) throws Exception {
        JdbcUtils.queryRunner().update(
                "update t_user set username=?,password=? where id=?",user.getUsername(),user.getPassword(),user.getId()
        );
    }

    @Override
    public void insert(User user) throws Exception {
        //添加用户
        JdbcUtils.queryRunner().update(
                "insert into t_user(username,password) values(?,?)",user.getUsername(),user.getPassword()
        );
    }

    @Override
    public User findUserByName(String username) throws Exception {
        return JdbcUtils.queryRunner().query(
                "select * from t_user where username=?",new BeanHandler<User>(User.class),username
        );
    }
}
